#include "rda5807m.h"
#include "cw32l010_i2c.h"
#include "cw32l010_gpio.h"
#include "cw32l010_systick.h"
#include "key.h"
#include "flash_fun.h"
fm_t fm_info = {
	.vol = 5,
};
// i2c GPIO 初始
void FM_GPIO_INIT(void)
{
	GPIO_InitTypeDef GPIO_InitStructure = {0};
	GPIO_InitStructure.Pins = I2C1_SCL_GPIO_PIN | I2C1_SDA_GPIO_PIN;
	GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
	GPIO_Init(CW_GPIOA, &GPIO_InitStructure);
	PA00_AFx_I2C1SDA();
	PA01_AFx_I2C1SCL();
}
void FM_I2C_INIT(void)
{
	I2C_InitTypeDef I2C_InitStruct = {0};
	// I2C初始化
	I2C_InitStruct.PCLK_Freq = 16000000;
	I2C_InitStruct.I2C_SCL_Freq = 100000;
	I2C_InitStruct.I2C_SCL_Source = I2C_SCL_SRC_GPIO;
	I2C_InitStruct.I2C_SDA_Source = I2C_SDA_SRC_GPIO;
	I2C1_DeInit();
	I2C_Master_Init(CW_I2C1, &I2C_InitStruct); // 初始化模块
	I2C_AcknowledgeConfig(CW_I2C1, ENABLE);	   // ACK打开
	I2C_Cmd(CW_I2C1, ENABLE);				   // 模块使能
}
void RDA5807M_WRITE(uint8_t reg, uint16_t data)
{
	uint8_t tmp[3] = {reg, data >> 8, data & 0xff};
	I2C_MasterSendDataToSlave(CW_I2C1, RDA5807_WR_ADDRESS, &tmp, 3);
}
uint16_t RDA5807M_READ(uint8_t addr)
{
	uint8_t data[2] = {0};
	uint16_t reg_data;
	I2C_MasterSendDataToSlave(CW_I2C1, RDA5807_RD_ADDRESS, &addr, 1);
	I2C_MasterRecDataFromSlave(CW_I2C1, RDA5807_RD_ADDRESS, &data, 2);
	reg_data = data[0] << 8 | data[1];
	return reg_data;
}
// 读取CHIP_ID
void FM_READ_CHIP(void)
{
	fm_info.chip_id = RDA5807M_READ(0x00);
}
// 0-15
void RDA5807M_SetVolume(unsigned char val)
{
	if (val >= 15) val = 15;
	if (val <= 1) val = 1;
	uint16_t readReg;
	readReg = RDA5807M_READ(0x05);
	readReg &= 0xFFF0;
	readReg |= (val & 0x0F);
	RDA5807M_WRITE(0x05, readReg);
	fm_info.vol = val;
}
// 音量等级（0~15）
uint8_t RDA5807M_GetVolume(void)
{
	uint16_t readReg;
	readReg = RDA5807M_READ(0x05);
	return readReg & 0x000F;
}
// 获取0AH中的chan值,返回值chan
uint16_t RDA5807M_GETCHAN(void)
{
	uint16_t chan;
	chan = RDA5807M_READ(0x0A);
	chan = chan & 0x03FF;
	return chan;
}
uint8_t RDA5807M_GETRSSI(void)
{
	uint16_t data;
	data = RDA5807M_READ(0x0B);
	data &= 0xFE00; // 保留15:9位，清零8:0位
	return data >> 9;
}
// 返回： 0，不是电台  1，是电台
uint8_t RDA5807M_IsRadio(void)
{
	return (RDA5807M_READ(0x0B) >> 8) & 0x0001;
}
// 获取电台信号强度
uint8_t RDA5807M_GetRSSI(void)
{
	return (RDA5807M_READ(0x0B) >> 9) & 0x007F;
}
// 开启搜索（向上搜索、开启搜索、到达最高频率截止）
void RDA5807M_SearchUp(void)
{
	uint16_t readReg;
	readReg = RDA5807M_READ(0x02);
	readReg |= 1 << 9; // 向上搜索
	readReg |= 1 << 8; // 开启搜索
	readReg |= 1 << 7; // 到达最高频率截止
	RDA5807M_WRITE(0x02, readReg);
}
// 向下搜索
void RDA5807M_SearchDown(void)
{
	uint16_t readReg;
	readReg = RDA5807M_READ(0x02);
	readReg &= ~(1 << 9); // 向下搜索
	readReg |= 1 << 8;	  // 开启搜索
	readReg |= 1 << 7;	  // 到达最高频率截止
	RDA5807M_WRITE(0x02, readReg);
}
// 获取RDA5807M波段
uint8_t RDA5807M_GetBand(void)
{
	uint16_t readReg;
	readReg = RDA5807M_READ(0x03);
	return (readReg >> 2) & 0x03;
}
// 获取RDA5807M频率间隔
uint8_t RDA5807M_GetFreqSpace(void)
{
	uint16_t readReg;
	readReg = RDA5807M_READ(0x03);
	return readReg & 0x03;
}
// 禁用调谐
void RDA5807M_DisableTune(void)
{
	uint16_t readReg = RDA5807M_READ(0x03);
	readReg &= ~(1 << 4);
	RDA5807M_WRITE(0x03, readReg);
}
// 搜索下一个电台
void RDA5807M_Search_Freq_TurnUp(void)
{
	fm_info.search_flag = 1;
	RDA5807M_DisableTune(); // 禁用调谐
	RDA5807M_SearchUp();	// 开启搜索（向上搜索、开启搜索、到达最高频率截止）
}
void RDA5807M_Search_Freq_TurnDown(void)
{
	fm_info.search_flag = 1;
	RDA5807M_DisableTune(); // 禁用调谐
	RDA5807M_SearchDown();	// 开启搜索（向上搜索、开启搜索、到达最高频率截止）
}
//0关闭bass,1开启bass
void RDA5807M_Bass(uint8_t ctl){
	if(ctl) fm_info.RDA5807_REG_02|=(1<<12);
	else fm_info.RDA5807_REG_02 &= ~(1<<12);
	RDA5807M_WRITE(0x02, fm_info.RDA5807_REG_02);
}
//0双声道,1单声音道
void RDA5807M_Mono(uint8_t ctl){
	if(ctl) fm_info.RDA5807_REG_02|=(1<<13);
	else fm_info.RDA5807_REG_02 &= ~(1<<13);
	RDA5807M_WRITE(0x02, fm_info.RDA5807_REG_02);
}
//band设置 0 全球，1 us/eu
void RDA5807M_band(uint8_t ctl){
	if(ctl) fm_info.RDA5807_REG_03 &=~(3<<2);
	else fm_info.RDA5807_REG_03 |=(2<<2);
	RDA5807M_WRITE(0x03, fm_info.RDA5807_REG_03);
}
//步进设置 0 200; 1 100；2 50; 3;25
void RDA5807M_Step(uint8_t ctl){
	if(ctl == 1) fm_info.RDA5807_REG_03 &= ~(3); //100khz 00
	if(ctl == 2) {
		fm_info.RDA5807_REG_03 &= ~(3);//先清空低两位
		fm_info.RDA5807_REG_03 |= 2; //50khz 00
	}
	if(ctl == 3) fm_info.RDA5807_REG_03 |=3; //25khz 11
	if(ctl==0){
		fm_info.RDA5807_REG_03 &= ~(3);//先清空低两位
		fm_info.RDA5807_REG_03 &=(1); //200khz 01
	}
	RDA5807M_WRITE(0x03, fm_info.RDA5807_REG_03);
}
//seek mode ,0 默认，1 RSSI
void RDA5807M_SeekMode(uint8_t ctl){
	if(ctl) fm_info.RDA5807_REG_05 |= (1<<14); //rssi搜索模式
	else fm_info.RDA5807_REG_05 &=~(1<<14); //默认搜索模式
	RDA5807M_WRITE(0x05, fm_info.RDA5807_REG_05);
}
//Lnaport ,0 LNAN，1 LNAP,2 dual
void RDA5807M_LnaMode(uint8_t ctl){
	if(ctl==1) {
		fm_info.RDA5807_REG_05 &= ~(3<<6); //清空
		fm_info.RDA5807_REG_05 |= (1<7); //lnAp
	}
	if(ctl==2) fm_info.RDA5807_REG_05 |= (3<<6); //dual
	if(ctl==0){
		fm_info.RDA5807_REG_05 &= ~(3<<6); //清空
		fm_info.RDA5807_REG_05 |= (1<<6); //LNAN
	} 
	RDA5807M_WRITE(0x05, fm_info.RDA5807_REG_05);
}
//LnaCurrent电流 ,0:1.8ma;1:2.1ma;2:2.5ma;3:3ma
void RDA5807M_LnaCurrent(uint8_t ctl){
	if(ctl==0) fm_info.RDA5807_REG_05 &= ~(3<<4); //1.8ma 00
	if(ctl==1) {
		fm_info.RDA5807_REG_05 &= ~(3<<4); //clear
		fm_info.RDA5807_REG_05 |= (1<<4); //01 2.1ma
	}
	if(ctl==2) {
		fm_info.RDA5807_REG_05 &= ~(3<<4); //claer
		fm_info.RDA5807_REG_05 |= (1<<5); //10 2.5ma
	}
	if(ctl==3) fm_info.RDA5807_REG_05 |= (3<<4); //3.0ma 11
	RDA5807M_WRITE(0x05, fm_info.RDA5807_REG_05);
}
//搜台SNR间隔，最大15
void RDA5807M_SeekTH(uint8_t th){
	fm_info.RDA5807_REG_05 &= ~(15<<8); //clear 11-8
	fm_info.RDA5807_REG_05 |= (th<<8); 
	RDA5807M_WRITE(0x05, fm_info.RDA5807_REG_05);
}
// RDA5807M初始化
void RDA5807M_Init(void)
{
	SysTickDelay(100);
	RDA5807M_WRITE(0x02, 0x0002); // 软重启
	SysTickDelay(50);
	// RDA5807M_WRITE(0x02, 0xC001);//启动
	RDA5807M_WRITE(0x02, 0xE005); // 不带低音增强,新解析启用
	SysTickDelay(600);
	FM_READ_CHIP();
	RDA5807M_WRITE(0x03, 0x0018); // 设置频率参数,开启协调，00全球波段，步进00 100khz
	RDA5807M_WRITE(0x04, 0x0400); // 软件静音开启，AFCD禁用
	RDA5807M_WRITE(0x05, 0x848B); // 搜台SNR 4db,lan模式选择AP，lan电流选择 1.8ma，音量控制
	RDA5807M_WRITE(0x06, 0x8000);
	RDA5807M_WRITE(0x07, 0x5F1A);
	SysTickDelay(600);
	RDA5807M_SetVolume(fm_info.vol);
	// RDA5807M_Search_Freq_TurnUp();
}
void FM_GET_INFO(void)
{
	if (fm_info.search_flag)
	{
		// SysTickDelay(50);
		if ((RDA5807M_READ(0x0A) & 0x4000) == 0)
		{
			// SysTickDelay(50);
			fm_info.freq = (RDA5807M_GETCHAN() * 100 + 76000) / 1000.0; // 转换为频率
			/* code */
		}
		else
			fm_info.search_flag = 0;
	}
	fm_info.rssi = RDA5807M_GETRSSI();
}


